﻿; (function () {

    'use strict';

    var compress = function (input) {
        return input;
    }

    var decompress = function (input) {
        return input;
    }

    window.UndoRedoManager = function UndoRedoManager(initialState) {
        this.stack = [];
        this.cursor = 0;
        this.canUndo = false;
        this.canRedo = false;
        if (initialState)
        {
            this.addState(initialState);
        }
    }

    UndoRedoManager.prototype.addState = function (data) {
        data = compress(data);
        if (data !== this.stack[this.cursor]) {
            this.stack.splice(this.cursor + 1);
            this.stack.push(data);
            this.cursor = this.stack.length - 1;
            this.updateCanUndoRedo();
        }
    }

    UndoRedoManager.prototype.undo = function () {
        if (!this.canUndo)
            return null;

        var value = decompress(this.stack[--this.cursor]);
        this.updateCanUndoRedo();
        return value;
    }

    UndoRedoManager.prototype.redo = function () {
        if (!this.canRedo)
            return null;

        var value = decompress(this.stack[++this.cursor]);
        this.updateCanUndoRedo();
        return value;
    }

    UndoRedoManager.prototype.updateCanUndoRedo = function () {
        this.canUndo = this.cursor > 0;
        this.canRedo = this.stack.length - 1 > this.cursor;
    }

}(window));
